package com.hspedu.inputStream;


import com.hspedu.outputstream.Dog;

import java.io.*;

/**
 * @Author Stringzhua
 * @Date 2024/3/17 10:41
 * description:
 */
public class ObjectInputStream_ {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //指定反序列化的文件
        String filePath = "e:\\qiyeke\\filecontroller\\data.dat";

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
        //读取
        //1.读取(反序列化)的顺序需要和你保存数据(序列化)的顺序一致
        //2.否则会出现异常
        System.out.println(ois.readInt());
        System.out.println(ois.readBoolean());
        System.out.println(ois.readChar());
        System.out.println(ois.readDouble());
        System.out.println(ois.readUTF());
        //dog的编译类型是Object,dog的运行类型是Dog
        Object dog = ois.readObject();
        System.out.println("运行类型" + ois.getClass());
//        System.out.println("Dog信息=" + ois.readObject());//底层Object-->Dog
        System.out.println("Dog信息=" + dog);

        //这里是特别重要的细节:
        //1.如果我们希望调用Dog的方法,需要向下转型
        //2.需要我们将Dog类的定义,拷贝到可以引用的位置
        Dog dog2 = (Dog) dog;
        System.out.println(dog2.getName());//旺财...
//        关闭流,关闭外层流即可,底层会关闭传入的字节流FileInputStream
        ois.close();
    }
}
